<?php
function changeToDu($dfm)
{
    if(preg_match('/[nNeE]/',$dfm)){
        $dfm=preg_replace('/[nNeE]/','',$dfm);
        $flag=1;
    }
    if(preg_match('/[sSwW]/',$dfm)){
        $dfm=preg_replace('/[sSwW]/','',$dfm);
        $flag=-1;
    }
    $arr1 = explode('°', $dfm);
    $d = $arr1[0];
    $arr2 = explode("'", $arr1[1]);
    $f = $arr2[0] ?? 0;
    $m = $arr2[1] ?? 0;
    $f = floatval($f) + floatval($m / 60);
    $du = floatval($f / 60) + floatval($d);
    return $du*$flag;
}

function changeToDFM($du)
{
    $str1 = explode(".", $du);
    $du1 = $str1[0];
    $tp = floatval("0." . $str1[1]);
    $tp = strval($tp * 60);//这里进行了强制类型转换
    $str2 = explode(".", $tp);
    $fen = $str2[0];
    $tp = "0." . $str2[1];
    $tp = $tp * 60;
    $miao = $tp;
    return $du1 . "°" . $fen . "'" . $miao . "\"";
}

//,94°16'00.56"e
$dfm='42°39\'33.30"n';
$du= changeToDu($dfm);
var_dump($dfm);
var_dump($du);
$dfm2=changeToDFM($du);
var_dump($dfm2);
echo '<hr />';
$dfm='94°16\'00.56"e';
$du= changeToDu($dfm);
var_dump($dfm);
var_dump($du);
$dfm2=changeToDFM($du);
var_dump($dfm2);